<?php
//后台用户

namespace App\Services;

use App\Models\AdminModel;
use App\Repositories\ManageUsersRepository;

class ManageUserService
{

    /**
     * 获取列表
     * @param $query
     * @param $offset
     * @param $limit
     * @param $orderType
     * @return array|mixed
     */
    public static function getList($query = [], $offset = 0, $limit = 20, $orderType = 0)
    {
        //获取证书关联信息
        $repo = new ManageUsersRepository();
        $repo->buildQuery($query);

        return $repo
            ->getList($offset, $limit, $orderType)
            ->toarray();
    }

    /**
     * 获取单条
     * @param $query
     * @return array
     */
    public static function getRow($query = [])
    {
        //获取证书关联信息
        $repo = new ManageUsersRepository();
        $repo->buildQuery($query);

        return $repo
            ->getRow();
    }

    /**
     * 获取总数
     * @param $query
     * @return array
     */
    public static function getTotal($query = [])
    {
        //获取证书关联信息
        $repo = new ManageUsersRepository();
        $repo->buildQuery($query);

        return $repo->getTotal();
    }

    /**
     * 获取页面结构列表
     * @param $query
     * @param $page
     * @param $pageSize
     * @param $orderType
     * @return int[]
     */
    public static function getPageList($query = [], $page = 1, $pageSize = 20, $orderType = 0)
    {
        $output = [
            'total'     => 0,
            'totalPage' => 0,
            'pageSize'  => (int) $pageSize,
            'list'      => [],
        ];
        //获取证书关联信息
        $repo = new ManageUsersRepository();
        $repo->buildQuery($query);
        $total = $repo->getTotal();
        if (!$total) {
            return $output;
        }
        $offset = ($page - 1) * $pageSize;
        $output['total'] = $total;
        $output['totalPage'] = (int) ceil($total / $pageSize);
        $list = $repo
            ->getList($offset, $pageSize, $orderType)
            ->toarray();
        //格式化列表信息
        $output['list'] = $list;

        return $output;
    }

    /**
     * 获取加密密码
     * @param $password
     * @param $salt
     * @return string
     */
    public static function getEncryptPassword($password, $salt)
    {

        return md5($password.$salt);
    }

    /**
     * 根据id获取用户信息
     * @param $id
     * @return array
     */
    public static function getUsreInfoById($id)
    {
        $res = self::getRow([
            'id' => $id,
        ]);

        return $res ? $res->toArray() : [];
    }

    /**
     * 更新密码
     * @param $id
     * @param $password
     * @return bool
     */
    public static function updatePassword($id, $password, $salt)
    {
        if (!$id || !$password) {
            return false;
        }
        $password = self::getEncryptPassword($password, $salt);
        $res = (new ManageUsersRepository())->updateFields($id, [
            'password' => $password,
        ]);
        if (!$res) {
            return false;
        }

        return true;
    }

    /**
     * 登录错误次数自增
     * @param $id
     * @param $num
     * @return bool
     */
    public static function incrLoginErrNum($id, $reset = false)
    {
        $admin = AdminModel::find($id);
        if (!$admin) {
            return false;
        }
        if ($reset) {
            $admin->error_login_number = 0;
            $res = $admin->save();
        } else {
            $res = $admin->increment('error_login_number', 1);
        }
        if (!$res) {
            return false;
        }

        return true;
    }

    /**
     * 更新登录错误次数
     * @param $id
     * @param $password
     * @return bool
     */
    public static function updateLoginErrNum($id, $num = 0)
    {
        if (!$id) {
            return false;
        }
        $res = (new ManageUsersRepository())->updateFields($id, [
            'login_err_num' => $num,
        ]);
        if (!$res) {
            return false;
        }

        return true;
    }


}
